We are going to make some plotly plots.
library(tidyverse)
library(p8105.datasets)
library(plotly)
let’s get some data.
data("nyc_airbnb")
nyc_airbnb |>
mutate(rating = review_scores_location / 2) |>
select(
borough = neighbourhood_group, neighbourhood, price, room_type,
lat, long, rating) |>
filter(
borough == "Manhattan",
room_type == "Entire home/apt",
price %in% 100:500
) |>
drop_na(rating)
## # A tibble: 7,568 × 7
## borough neighbourhood price room_type lat long rating
## <chr> <chr> <dbl> <chr> <dbl> <dbl> <dbl>
## 1 Manhattan Battery Park City 165 Entire home/apt -74.0 40.7 4.5
## 2 Manhattan Battery Park City 225 Entire home/apt -74.0 40.7 5
## 3 Manhattan Battery Park City 299 Entire home/apt -74.0 40.7 5
## 4 Manhattan Battery Park City 168 Entire home/apt -74.0 40.7 5
## 5 Manhattan Battery Park City 100 Entire home/apt -74.0 40.7 5
## 6 Manhattan Battery Park City 225 Entire home/apt -74.0 40.7 5
## 7 Manhattan Battery Park City 250 Entire home/apt -74.0 40.7 5
## 8 Manhattan Battery Park City 110 Entire home/apt -74.0 40.7 4.5
## 9 Manhattan Battery Park City 249 Entire home/apt -74.0 40.7 5
## 10 Manhattan Battery Park City 325 Entire home/apt -74.0 40.7 5
## # … with 7,558 more rows
Lets make a scatterplot !!
nyc_airbnb |>
mutate(
text_label = str_c("Price: ", price)
) |>
plot_ly(
x = ~lat, y = ~long, color = ~ price,
type = "scatter", mode = "markers", alpha = .5, text = ~text_label
)
Can we make boxplots? sure can!
nyc_airbnb |>
mutate(neighbourhood = fct_reorder(neighbourhood, price)) |>
plot_ly(
y = ~price, color = ~neighbourhood,
type = "box", colors = "viridis")
Can we make a bar plot?
nyc_airbnb |>
count(neighbourhood) |>
mutate(neighbourhood = fct_reorder(neighbourhood, n)) |>
plot_ly(x = ~neighbourhood, y = ~n,
type = "bar")
ggp_scatterplot =
nyc_airbnb |>
ggplot(aes(x = lat, y = long, color = price)) +
geom_point()
ggplotly(ggp_scatterplot)
Not here though.